package com.wjyxsy.job;

import com.wjyxsy.entity.TimeEntity;
import com.wjyxsy.filter.TPMonitorFilter;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;


@Component
@Configuration
@EnableAsync
@EnableScheduling
public class QuartzJob {

    private AtomicInteger count = new AtomicInteger(0);

    @Scheduled(initialDelay=5000,fixedDelay=5000)
    public void doSomething() {
        count.incrementAndGet();
        System.out.println("每隔5s定时任务开始......");
        BlockingDeque<Long> queue1 = TPMonitorFilter.queue1;
        BlockingDeque<Long> queue2 = TPMonitorFilter.queue2;
        BlockingDeque<Long> queue3 = TPMonitorFilter.queue3;

        Object[] timeEntities1 = queue1.toArray();
        Object[] timeEntities2 = queue2.toArray();
        Object[] timeEntities3 = queue3.toArray();

        if (timeEntities1.length == 0
            || timeEntities2.length == 0
            || timeEntities3.length == 0
        ) {
            return;
        }

        Arrays.sort(timeEntities1);
        Arrays.sort(timeEntities2);
        Arrays.sort(timeEntities3);

        Long timeEntity1_90 = (Long) timeEntities1[(int)Math.floor(timeEntities1.length * 0.9)];
        Long timeEntity2_90 = (Long) timeEntities2[(int)Math.floor(timeEntities2.length * 0.9)];
        Long timeEntity3_90 = (Long) timeEntities3[(int)Math.floor(timeEntities3.length * 0.9)];

        Long timeEntity1_99 = (Long) timeEntities1[(int)Math.floor(timeEntities1.length * 0.99)];
        Long timeEntity2_99 = (Long) timeEntities2[(int)Math.floor(timeEntities2.length * 0.99)];
        Long timeEntity3_99 = (Long) timeEntities3[(int)Math.floor(timeEntities3.length * 0.99)];

        System.out.println("method1 tp90=" + timeEntity1_90 + " | tp99=" + timeEntity1_99);
        System.out.println("method2 tp90=" + timeEntity2_90 + " | tp99=" + timeEntity2_99);
        System.out.println("method3 tp90=" + timeEntity3_90 + " | tp99=" + timeEntity3_99);
    }

    /*****
     * 格式: [秒] [分] [小时] [日] [月] [周] [年]
     0 0 12 * * ?           每天12点触发
     0 15 10 ? * *          每天10点15分触发
     0 15 10 * * ?          每天10点15分触发
     0 15 10 * * ? *        每天10点15分触发
     0 15 10 * * ? 2005     2005年每天10点15分触发
     0 * 14 * * ?           每天下午的 2点到2点59分每分触发
     0 0/5 14 * * ?         每天下午的 2点到2点59分(整点开始，每隔5分触发)
     0 0/5 14,18 * * ?        每天下午的 18点到18点59分(整点开始，每隔5分触发)

     0 0-5 14 * * ?            每天下午的 2点到2点05分每分触发
     0 10,44 14 ? 3 WED        3月分每周三下午的 2点10分和2点44分触发
     0 15 10 ? * MON-FRI       从周一到周五每天上午的10点15分触发
     0 15 10 15 * ?            每月15号上午10点15分触发
     0 15 10 L * ?             每月最后一天的10点15分触发
     0 15 10 ? * 6L            每月最后一周的星期五的10点15分触发
     0 15 10 ? * 6L 2002-2005  从2002年到2005年每月最后一周的星期五的10点15分触发

     0 15 10 ? * 6#3           每月的第三周的星期五开始触发
     0 0 12 1/5 * ?            每月的第一个中午开始每隔5天触发一次
     0 11 11 11 11 ?           每年的11月11号 11点11分触发(光棍节)
     */
//    @Scheduled(cron="0/5 * * * * ?")
//    public void doSomeWork(){
//        System.out.println("每隔5s定时任务开始......");
//    }
}
